package com.yidu.boss.aspect;

import com.yidu.boss.sys.controller.LogController;
import com.yidu.boss.sys.domain.Log;
import com.yidu.boss.sys.domain.User;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;

/**
 * @Author luoyushan
 * @Date 2019/11/27 14:43
 * 日志切面类
 */

@Component
@Aspect
public class LogAspect {
    @Autowired
    LogController logController;
    User user;
    /**
     * 定义切点
     */
    @Pointcut("execution(* com.yidu.boss.*.controller..*.do*(..))")
    public void  webLog(){

    }

    /**
     * 此法用于切入
     * @param jP
     */
    @After("webLog()")
    public void  doAfter(JoinPoint jP) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (user==null){
            user = (User) request.getAttribute("user");
        }
        //获取切入的方法名
        String methodName= jP.getSignature().getName();
        LogALG(request, user, "doLogin",methodName,"登录","基础->登录");

    }

    @Before("webLog()")
    public void doBefore(JoinPoint jP){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String methodName= jP.getSignature().getName();
        LogALG(request, user, "doSeeLog",methodName,"浏览日志","系统管理->日志");
        LogALG(request, user, "doSelectAllExchange",methodName,"浏览品种费率","基本->费率");
        LogALG(request, user, "doSelectAllCustodian",methodName,"浏览管理人","基本->管理人");
        LogALG(request, user, "doSelectAllTrustee",methodName,"浏览托管人","基本->托管人");

    }

    /**
     * 日志插入方法
     * @param request 请求对象
     * @param user 用户对象
     * @param targetMethod 目标方法
     * @param methodName 正在执行的方法名
     * @param motion 事件
     * @param source 资源
     */
    private void LogALG(HttpServletRequest request, User user,String  targetMethod,String methodName,String motion,String source) {
        if (targetMethod.equals(methodName)){
            if (user!=null){
                Log log = new Log();
                String clientIp = getClientIp(request);
                log.setComputerIP(clientIp);
                log.setMotion(motion);
                log.setUserId(user.getUserId());
                log.setUserName(user.getUserName());
                log.setLogTime(new Date());
                log.setSource(source);
                logController.insertLog(log);
            }
        }
    }

    /**
     * 获取客户端的IP地址
     *
     * @return
     */
    public String getClientIp(HttpServletRequest request) {
        String ipAddress = null;
        ipAddress = request.getHeader("x-forwarded-for");
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
            if (ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")) {
                // 根据网卡取本机配置的IP
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                if (inet != null) {
                    ipAddress = inet.getHostAddress();
                }
            }

        }

        // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        if (ipAddress != null && ipAddress.length() > 15) {
            if (ipAddress.indexOf(",") > 0) {
                ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
            }
        }
        return ipAddress;
    }


}
